package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.vo.WebOrder;
import com.clkj.xiuzheng.service.shangcheng.PayService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/05/17 10:32:58
 **/
@RestController
@RequestMapping(value = "/pay")
@CrossOrigin
@Api(value = "/pay", tags = {"Z商城支付管理"})
public class PayController {
    @Autowired
    private PayService payService;


    @ApiOperation(value = "小程序订单支付生成微信预支付信息")
    @PostMapping("/wxPay")
    public Result wxPay( @ApiParam(name = "openId", value = "openId")@RequestParam("openId") String  openId,
                         @ApiParam(name = "totalMoney", value = "总金额")@RequestParam("totalMoney") String  totalMoney,
                         @ApiParam(name = "orderNum", value = "订单号")@RequestParam("orderNum") String  orderNum,
                         @ApiParam(name = "userId", value = "用户id")@RequestParam("userId") Integer   userId,
                         HttpServletRequest request) {

        try {
            Map map  = payService.wxPay(openId,totalMoney,orderNum,userId,request);
            return new Result(true, StatusCode.OK,"生成成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"尚有未发货或未支付的订单");
        }
    }

    @ApiOperation(value = "小程序订单支付成功回调")
    @PostMapping("/paySuccess")
    public Result paySuccess(HttpServletRequest request, HttpServletResponse response) {

        try {
            payService.paySuccess(request,response);
            return new Result(true, StatusCode.OK,"生成成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @ApiOperation("小程序用户充值")
    @PostMapping("/userRecharge")
    public Result userRecharge(@ApiParam(name = "userId", value = "用户ID") @RequestParam("userId") Integer userId,
                               @ApiParam(name = "rechargeMoney", value = "充值金额") @RequestParam("rechargeMoney") String rechargeMoney,
                               @ApiParam(name = "scoreNum", value = "获得的积分总额") @RequestParam("scoreNum") Integer scoreNum,
                               @ApiParam(name = "diamondNum", value = "获得的钻石总额") @RequestParam("diamondNum") Integer diamondNum,
                               @ApiParam(name = "commissionNum", value = "给推广员的佣金") @RequestParam("commissionNum") String commissionNum,
                               HttpServletRequest request) {
        try {
            Map map = payService.userRecharge(userId,rechargeMoney,scoreNum,diamondNum,commissionNum,request);
            return new Result(true, StatusCode.OK,"充值成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    @ApiOperation("小程序用户充值支付成功回调")
    @PostMapping("/paySuccessRecharge")
    public Result paySuccessRecharge( HttpServletRequest request, HttpServletResponse response) {
        try {
            payService.paySuccessRecharge(request,response);
            return new Result(true, StatusCode.OK,"充值成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("网页版发起微信支付")
    @PostMapping("/webPay")
    public Result webPay( String goodsName,Integer price,Integer userId) {
        try {
            String codeUrl = payService.webPay(goodsName,price,userId);

            return new Result(true, StatusCode.OK,"充值成功",codeUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("网页版微信回调")
    @PostMapping("/webPaySuccess")
    public Result webPaySuccess(HttpServletRequest request, HttpServletResponse response) {
        try {
            payService.paySuccessWeb(request,response);
            return new Result(true, StatusCode.OK,"查询成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    @ApiOperation("查询用户订单记录")
    @PostMapping("/getUserByOrder")
    public Result getUserByOrder(Integer userId){
        try {
            List<WebOrder> webOrder = payService.getUserInfo(userId);
            return new Result(true, StatusCode.OK,"查询成功",webOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

}
